<template>
  <div v-loading.lock="loading"
    class="base-content">
    <div class="basic-info">
      <el-form ref="queryForm"
        :inline="true"
        :model="queryForm">
        <el-form-item label="检验单产品Id"
          prop="inspectionProductId">
          <el-input v-model="queryForm.inspectionProductId"
            placeholder="检验单产品Id" />
        </el-form-item>
        <el-form-item label="订单id"
          prop="orderId">
          <el-input v-model="queryForm.orderId"
            placeholder="订单id" />
        </el-form-item>
        <el-form-item label="产品id"
          prop="productId">
          <el-input v-model="queryForm.productId"
            placeholder="产品id" />
        </el-form-item>
        <el-form-item label="产品编号"
          prop="productNum">
          <el-input v-model="queryForm.productNum"
            placeholder="产品编号" />
        </el-form-item>
        <el-form-item label="合格数量"
          prop="qualifiedCount">
          <el-input v-model="queryForm.qualifiedCount"
            placeholder="合格数量" />
        </el-form-item>
        <el-form-item label="不合格数量"
          prop="unqualifiedCount">
          <el-input v-model="queryForm.unqualifiedCount"
            placeholder="不合格数量" />
        </el-form-item>
        <el-form-item label="已检数量"
          prop="inspectedCount">
          <el-input v-model="queryForm.inspectedCount"
            placeholder="已检数量" />
        </el-form-item>
        <el-form-item label="供应商id"
          prop="supplierId">
          <el-input v-model="queryForm.supplierId"
            placeholder="供应商id" />
        </el-form-item>
        <el-form-item label="供应商Name"
          prop="supplierName">
          <el-input v-model="queryForm.supplierName"
            placeholder="供应商Name" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
            @click="onQuery">查询</el-button>
          <el-button @click="cancelForm">清空</el-button>
        </el-form-item>
      </el-form>

      <div class="base-content-operate">
        <el-button icon="el-icon-plus"
          type="primary"
          @click="onNew">新增</el-button>
      </div>

      <el-table :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row>
        <el-table-column align="center"
          label="序号"
          width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="检验单产品Id">
          <template slot-scope="scope">
            {{ scope.row.inspectionProductId }}
          </template>
        </el-table-column>
        <el-table-column label="订单id">
          <template slot-scope="scope">
            {{ scope.row.orderId }}
          </template>
        </el-table-column>
        <el-table-column label="产品id">
          <template slot-scope="scope">
            {{ scope.row.productId }}
          </template>
        </el-table-column>
        <el-table-column label="产品编号">
          <template slot-scope="scope">
            {{ scope.row.productNum }}
          </template>
        </el-table-column>
        <el-table-column label="合格数量">
          <template slot-scope="scope">
            {{ scope.row.qualifiedCount }}
          </template>
        </el-table-column>
        <el-table-column label="不合格数量">
          <template slot-scope="scope">
            {{ scope.row.unqualifiedCount }}
          </template>
        </el-table-column>
        <el-table-column label="已检数量">
          <template slot-scope="scope">
            {{ scope.row.inspectedCount }}
          </template>
        </el-table-column>
        <el-table-column label="供应商id">
          <template slot-scope="scope">
            {{ scope.row.supplierId }}
          </template>
        </el-table-column>
        <el-table-column label="供应商Name">
          <template slot-scope="scope">
            {{ scope.row.supplierName }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link size="mini"
                type="primary"
                @click="handleEdit(scope.row)">编辑</el-link>
              <el-link size="mini"
                type="danger"
                @click="handleDelete(scope.row)">删除</el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination :total="pagination.total"
        :current-page="pagination.pageIndex"
        :page-count="pagination.total"
        @changePage="changePage"
        @sizeChange="sizeChange" />
    </div>

    <MaterialInspectionDetailsEdit ref="MaterialInspectionDetailsEdit"
      :dialog-form-visible.sync="dialogFormVisible" />
  </div>
</template>

<script>
import { deleteRecord, getList } from "../../../api/Material/MaterialInspectionDetails.js";
import MaterialInspectionDetailsEdit from "./MaterialInspectionDetailsEdit.vue";

export default {
  name: "MaterialInspectionDetailsList",
  components: { MaterialInspectionDetailsEdit },
  data() {
    return {
      loading: true,
      list: [],
      queryForm: {
        inspectionProductId: 0,
        orderId: 0,
        productId: 0,
        productNum: "",
        qualifiedCount: 0,
        unqualifiedCount: 0,
        inspectedCount: 0,
        supplierId: 0,
        supplierName: "",
      },
      dialogFormVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changePage(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.pagination.pageIndex = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onNew() {
      this.$refs.MaterialInspectionDetailsEdit.formData = this.Helper.deepCopy(
        this.$refs.MaterialInspectionDetailsEdit.initData
      );
      this.dialogFormVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    handleEdit(rowData) {
      this.$refs.MaterialInspectionDetailsEdit.formData = rowData;
      this.dialogFormVisible = true;
    },
    onQuery() {
      this.getList();
    },
  },
};
</script>
